package dialogs.game;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.Observable;

import persistence.Player;

public class GameInputModel extends Observable {

	private Player player1Team1;
	private Player player2Team1;
	private Player player1Team2;
	private Player player2Team2;

	private int goalsT1;
	private int goalsT2;
	private Date date;

	private boolean player1Team1Valid;
	private boolean player2Team1Valid;
	private boolean player1Team2Valid;
	private boolean player2Team2Valid;
	private boolean goalsT1Valid;
	private boolean goalsT2Valid;
	private boolean dateValid;

	public GameInputModel() {
		setPlayer1Team1(null);
		setPlayer2Team1(null);
		setPlayer1Team2(null);
		setPlayer2Team2(null);

		goalsT1 = 0;
		goalsT2 = 0;

		setPlayer1Team1Valid(false);
		setPlayer2Team1Valid(false);
		goalsT1Valid = false;
		goalsT2Valid = false;
		dateValid = false;
	}

	public int getGoalsT1() {
		return goalsT1;
	}

	public void setGoalsT1(String _toreT1) {
		try {
			this.goalsT1 = Integer.parseInt(_toreT1);
			this.setGoalsT1Valid(true);
		} catch (NumberFormatException e) {
			this.setGoalsT1Valid(false);
		}
		if (this.goalsT1 < 0)
			this.setGoalsT1Valid(false);
	}

	public int getGoalsT2() {
		return goalsT2;
	}

	public void setGoalsT2(String _toreT2) {
		try {
			this.goalsT2 = Integer.parseInt(_toreT2);
			this.setGoalsT2Valid(true);
		} catch (NumberFormatException e) {
			this.setGoalsT2Valid(false);
		}
		if (this.goalsT2 < 0) {
			this.setGoalsT2Valid(false);
		}
	}

	public Date getDate() {
		return date;
	}

	public void setDate(String dateString) {
		try {
			DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM,
					Locale.GERMANY);
			date = df.parse(dateString);
			this.setDateValid(true);
		} catch (ParseException pe) {
			date = new Date(0);
			// wenn sich Geburtsdatum nicht parsen laesst, ist es nicht valide
			this.setDateValid(false);
		}
	}

	public Player getPlayer1Team1() {
		return player1Team1;
	}

	public void setPlayer1Team1(Player player1Team1) {
		if (player1Team1 != null) {
			setPlayer1Team1Valid(true);
			this.player1Team1 = player1Team1;
		}
		else {
			setPlayer1Team1Valid(false);
		}
	}
	
	public Player getPlayer2Team1() {
		return player2Team1;
	}


	public void setPlayer2Team1(Player player2Team1) {
		if (player2Team1 != null) {
			setPlayer2Team1Valid(true);
			this.player2Team1 = player2Team1;
		}
		else {
			setPlayer2Team1Valid(false);
		}
	}

	public Player getPlayer1Team2() {
		return player1Team2;
	}

	public void setPlayer1Team2(Player player1Team2) {
		if (player1Team2 != null) {
			setPlayer1Team2Valid(true);
			this.player1Team2 = player1Team2;
		}
		else {
			setPlayer1Team2Valid(false);
		}
	}

	public Player getPlayer2Team2() {
		return player2Team2;
	}

	public void setPlayer2Team2(Player player2Team2) {
		if (player2Team2 != null) {
			setPlayer2Team2Valid(true);
			this.player2Team2 = player2Team2;
		}
		else {
			setPlayer2Team2Valid(false);
		}
	}
	
	/**
	 * 
	 * 
	 */
	public boolean isPlayersValid() {
		boolean check = true;
		if (!isPlayer1Team1Valid() && !isPlayer2Team1Valid())
			check = false;
		if (!isPlayer1Team2Valid() && !isPlayer2Team2Valid())
			check = false;
		if (isPlayer1Team1Valid()) {
			if (player1Team1 == player2Team1 || player1Team1 == player1Team2
					|| player1Team1 == player2Team2)
				check = false;
		}
		if (isPlayer2Team1Valid()) {
			if (player2Team1 == player1Team1 || player2Team1 == player1Team2
					|| player2Team1 == player2Team2)
				check = false;
		}
		if (isPlayer1Team2Valid()) {
			if (player1Team2 == player2Team1 || player1Team2 == player1Team1
					|| player1Team2 == player2Team2)
				check = false;
		}
		if (isPlayer2Team2Valid()) {
			if (player2Team2 == player1Team1 || player2Team2 == player2Team1
					|| player2Team2 == player1Team2)
				check = false;
		}
		return check;

	}


	public boolean isGoalsT1Valid() {
		return goalsT1Valid;
	}

	public void setGoalsT1Valid(boolean toreT1Valid) {
		this.goalsT1Valid = toreT1Valid;
		this.setChanged();
		this.notifyObservers();
	}

	public boolean isGoalsT2Valid() {
		return goalsT2Valid;
	}

	public void setGoalsT2Valid(boolean toreT2Valid) {
		this.goalsT2Valid = toreT2Valid;
		this.setChanged();
		this.notifyObservers();
	}

	public boolean isDateValid() {
		return dateValid;
	}

	public void setDateValid(boolean dateValid) {
		this.dateValid = dateValid;
		this.setChanged();
		this.notifyObservers();
	}

	public boolean isModelValid() {
		if (isPlayersValid() && isGoalsT1Valid()
				&& isGoalsT2Valid() && isDateValid())
			return true;
		else
			return false;
	}

	public boolean isPlayer1Team1Valid() {
		return player1Team1Valid;
	}

	public void setPlayer1Team1Valid(boolean player1Team1Valid) {
		this.player1Team1Valid = player1Team1Valid;
		this.setChanged();
		this.notifyObservers();
	}

	public boolean isPlayer2Team1Valid() {
		return player2Team1Valid;
	}

	public void setPlayer2Team1Valid(boolean player2Team1Valid) {
		this.player2Team1Valid = player2Team1Valid;
		this.setChanged();
		this.notifyObservers();
	}

	public boolean isPlayer1Team2Valid() {
		return player1Team2Valid;
	}

	public void setPlayer1Team2Valid(boolean player1Team2Valid) {
		this.player1Team2Valid = player1Team2Valid;
		this.setChanged();
		this.notifyObservers();
	}

	public boolean isPlayer2Team2Valid() {
		return player2Team2Valid;
	}

	public void setPlayer2Team2Valid(boolean player2Team2Valid) {
		this.player2Team2Valid = player2Team2Valid;
		this.setChanged();
		this.notifyObservers();
	}		
}
